package com.example.demo.controller;

import com.example.demo.common.ResponseMessage;
import com.example.demo.pojo.Address;
import com.example.demo.pojo.Customer;
import com.example.demo.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/address/")
public class AddressController {
    @Autowired
    private IAddressService iAddressService;

    @RequestMapping("add.do")
    public ResponseMessage register(Address address) {
        int res =  iAddressService.addAddress(address);
        ResponseMessage msg = null;
        if(res>0){
            msg = new ResponseMessage(0, "新建地址成功", address);
            return msg;
        }else{
            msg = new ResponseMessage(1, "新建地址失败");
            return msg;
        }
    }

    @RequestMapping("del.do")
    public ResponseMessage deleteAddress(Integer id) {
        int res =  iAddressService.deleteAddress(id);
        ResponseMessage msg = null;
        if(res != 0){
            msg = new ResponseMessage(0, "删除地址成功");
            return msg;
        }else{
            msg = new ResponseMessage(1, "删除地址失败");
            return msg;
        }
    }
    @RequestMapping("update.do")
    public ResponseMessage updateAddress(Address address) {
        int res =  iAddressService.updateAddress(address);
        ResponseMessage msg = null;
        if(res > 0){
            msg = new ResponseMessage(0, "更新地址成功",address);
            return msg;
        }else{
            msg = new ResponseMessage(1, "更新地址失败");
            return msg;
        }
    }
    @RequestMapping("select.do")
    public ResponseMessage selectAddress(Integer id, HttpSession session){
        Address address = iAddressService.selectAddress(id);
        Customer c = (Customer) session.getAttribute("currentCustomer");
        ResponseMessage msg = null;
        if(c != null){
            msg = new ResponseMessage(0,"查询成功",address);
            return msg;
        }else{
            msg = new ResponseMessage(1, "请登陆之后查询");
            return msg;
        }
    }

    @RequestMapping("list.do")
    public ResponseMessage getAddressList(@RequestParam(defaultValue = "1") int nowPage, @RequestParam(defaultValue = "10") int pageSize, HttpSession session){
        Customer c = (Customer) session.getAttribute("currentCustomer");
        ResponseMessage msg = null;
        if(c != null){
            msg = iAddressService.getAddressList(nowPage, pageSize);
            return msg;
        }else{
            msg = new ResponseMessage(1, "请登陆之后查询");
            return msg;
        }
    }
}
